<template>
  <div class="list-table">
    <div class="nospecialModel ">
      <div class="area-header">
        <span class="area-header-title">{{ $t('异常经营') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('列入日期')" prop="putDateArray">
          <DatePicker
            v-model="putDateArray"
            type="daterange"
          />
        </el-form-item>
        <el-form-item :label="$t('列入部门')" prop="putDept">
          <el-input
            v-model="queryParams.putDept"
            :placeholder="$t('列入部门')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['company:abnormal:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
      </el-row>
    </div>

    <div class="specialModel">
      <!-- 不可删除 -->
      <div class="area-header left">
        <span class="area-header-title">{{ $t('异常经营') }}</span>
      </div>
      <div class="right">
        <el-button
          v-if="sourceType !== 'bussRisk'"
          v-hasPermi="['company:abnormal:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </div>
    </div>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="companyAbnormalList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('列入部门')" align="center" prop="putDept" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link class="link_a" type="primary" @click="updateHandle(scope.row)">{{ scope.row.putDept }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('列入日期')" align="center" prop="putDate" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.putDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('列入原因')" align="center" prop="putReason" show-overflow-tooltip />
      <el-table-column :label="$t('移出日期')" align="center" prop="removeDate" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.removeDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('移出部门')" align="center" prop="removeDept" show-overflow-tooltip />
      <el-table-column :label="$t('移出原因')" align="center" prop="removeReason" show-overflow-tooltip />
      <el-table-column v-if="sourceType !== 'bussRisk'" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:abnormal:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改异常经营对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <companyAbnormalForm ref="companyAbnormalForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyAbnormal, delCompanyAbnormal, exportCompanyAbnormal } from '@/api/company/abnormal/companyAbnormal'
import companyAbnormalForm from './companyAbnormalForm.vue'
import moment from 'moment'
export default {
  components: {
    companyAbnormalForm
  },
  props: {
    sourceType: {
      type: String,
      default: ''
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 异常经营表格数据
          companyAbnormalList: [],
          // 弹出层标题
          title: '异常经营',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数 时间数组
          putDateArray: [],
          // 查询参数
          queryParams: {
              companyId: undefined,
              putDate: undefined,
              putDept: undefined,
              putDateStart: undefined,
              putDateEnd: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {}
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询异常经营列表 */
    getList() {
        this.loading = true
        this.queryParams.companyId = this.$route.query.companyId
        if (this.putDateArray !== undefined) {
          if (this.putDateArray[0] !== null && this.putDateArray[0] !== undefined) {
            this.queryParams.putDateStart = moment(this.putDateArray[0]).format('YYYY-MM-DD')
          }
          if (this.putDateArray[1] !== null && this.putDateArray[1] !== undefined) {
            this.queryParams.putDateEnd = moment(this.putDateArray[1]).format('YYYY-MM-DD')
          }
        }
        listCompanyAbnormal(this.queryParams).then(response => {
            this.companyAbnormalList = response.rows
            this.total = response.total
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: undefined,
            putDate: undefined,
            putDept: undefined,
            putReason: undefined,
            removeDate: undefined,
            removeDept: undefined,
            removeReason: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.putDateArray = []
        this.queryParams.putDateStart = undefined
        this.queryParams.putDateEnd = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.companyAbnormalForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.companyAbnormalForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delCompanyAbnormal(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportCompanyAbnormal(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
